#!/bin/sh
# - runtime
# - nr_processes
# - nr_threads
# - cpu_node_bind
# - mem_node_bind
# - mem_blk_size
# - mem_total_size
# - mem_scope
# - mem_hugetlb
# - mem_operation
# - mem_access_mode
# - rand_type
# - rand_pareto_h

. "$LKP_SRC/lib/reproduce-log.sh"
. "$LKP_SRC/lib/numactl.sh"

: "${runtime:=600}"
: "${nr_processes:=1}"
: "${nr_threads:=1}"
: "${mem_blk_size:=1K}"
: "${mem_total_size:=100G}"
: "${mem_scope:=global}"
: "${mem_hugetlb:=off}"
: "${mem_opt:=write}"
: "${mem_access_mode:=seq}"

args="\
 --threads=$nr_threads \
 --time=$runtime \
 memory \
 --memory-block-size=$mem_blk_size \
 --memory-total-size=$mem_total_size \
 --memory-scope=$mem_scope \
 --memory-hugetlb=$mem_hugetlb \
 --memory-oper=$mem_operation \
 --memory-access-mode=$mem_access_mode \
"

[ -n "$rand_type" ] && args="$args --rand-type=$rand_type"
[ -n "$rand_pareto_h" ] && args="$args --rand-pareto-h=$rand_pareto_h"

sb_tmp_dir="$TMP/sysbench-memory"
rm -rf "$sb_tmp_dir"
mkdir -p "$sb_tmp_dir"

parse_numa_node_binding "$cpu_node_bind" "$mem_node_bind"

for i in $(seq 1 $nr_processes); do
	numa_bind=$(numa_node_binding "$i")
	log_echo "$exec_prefix $numa_bind sysbench $args run > $sb_tmp_dir/sysbench-memory.$i &"
	$numa_bind sysbench $args run > "$sb_tmp_dir/sysbench-memory.$i" &
done

wait

cat "$sb_tmp_dir"/sysbench-memory.*
